<?php
    include_once("../../includes/config.inc.php");
    check_login();
    $suserSession=get_suser();
    $smarty->assign('suserSession',$suserSession);
    $customer_id=$suserSession['id'];
    $model=D('member');

    //移动用户到分组
    if($_REQUEST['a']=='changeGroup'){
        $openid=array_filter($_POST['openid']);
        $groupid=$_REQUEST['groupId'];
        $configModel=new Model_CustomerConfig();
        $token=$configModel->getToken($customer_id);
        $res=$configModel->changeUserGroups($token,$openid,$groupid);

        if($res){
            $where="where fromuser in('".implode("','",$openid)."')";
            $configModel->query("update member set group_id = '{$groupid}' where customer_id = '{$customer_id}' and fromuser in('".implode("','",$openid)."')");//更新会员表
            die('suc');
        }else{
            die('err');
        }
    }

    //修改备注
    if($_REQUEST['a']=='edit_des'){
        $dat=array();
        $dat['num']['id']=$_POST['id'];
        $dat['str']['des']=$_POST['des'];
        $res=$model->add($dat);
        if($res) echo 'suc';
        die;
    }

    //数据列表
    $condition=array();
    $customerModel=D('customer');
    //总部账号
    if($suserSession['type']==1){
        //所有门店
        $customerList=$customerModel->where("dele_state='1'")->dataArr();
        $smarty->assign('customerList',$customerList);
        
        $shopArr=array();$shopRow=array();
        if($customerList){
            foreach($customerList as $k=>$v){
                $shopArr[]=$v['id'];
                $shopRow[$v['id']]=$v['name'];
            }
            $condition[] = " customer_id in (".implode(',',$shopArr).") ";
        }else{
            $condition[] = " customer_id = -1 ";
        }
    //区域账号
    }elseif($suserSession['type']==2){
        //区域下的门店
        $customerList=$customerModel->where("pid='".$customer_id."' and dele_state='1'")->dataArr();
        $smarty->assign('customerList',$customerList);
        
        $shopArr=array();$shopRow=array();
        if($customerList){
            foreach($customerList as $k=>$v){
                $shopArr[]=$v['id'];
                $shopRow[$v['id']]=$v['name'];
            }
            $condition[] = " customer_id in (".implode(',',$shopArr).") ";
        }else{
            $condition[] = " customer_id = -1 ";
        }
    //门店账号
    }elseif($suserSession['type']==3){
        //获取用户分组
        $groupRow=array();
        $configModel=new Model_CustomerConfig();
        $groupArr=$configModel->getGroups($customer_id);
        foreach($groupArr as $v){
            $groupRow[$v['id']]=$v['name'];
        }
        $smarty->assign('groupRow',$groupRow);
        $condition[] = " customer_id='{$customer_id}' ";
    }
    
    if($_GET['sex']) $condition[]=" sex = '".common_pg('sex')."' ";
    if($_GET['p_c'])$condition[] = " (province like '%".common_pg('p_c')."%' or city like '%".common_pg('p_c')."%') ";
    if($_GET['group_id']=='-1'){
        $condition[]=" group_id = '0' ";
    }elseif($_GET['group_id']){
        $condition[]=" group_id = '".$_GET['group_id']."' ";
    }
    if($_GET['nickname']) $condition[]=" nickname like '%".common_pg('nickname')."%' ";
    //所在门店的搜索
    if($_GET['shop_id']) $condition[]=" customer_id = '".common_pg('shop_id')."' ";

    if($condition) $filter['where'] = implode('and',$condition);

    $filter['order'] = "id desc";
    $data = $model->paginate($filter,'*',common_pg('p'),10);
    $listArr = $data['data'];

    foreach($listArr as $key=>$val){
        $listArr[$key]['subscribe_time']=$val['subscribe_time'] ? date('Y-m-d',$val['subscribe_time']).'<br />'.date('H:i:s',$val['subscribe_time']) : '-';
        $listArr[$key]['group_name']=$groupRow[$val['group_id']];
        $listArr[$key]['shop_name']=$shopRow[$val['customer_id']];
    }
    $smarty->assign('list',$listArr);
    $smarty->assign('page',$model->pager($data['pager']));


    $smarty->setTpl('member/templates/list.html')->display();